

local jiexun = fk.CreateSkill{
  name = "wk_heg__jiexun",
}

Fk:loadTranslationTable{
  ["wk_heg__jiexun"] = "诫训",
  [":wk_heg__jiexun"] = "与你势力相同角色使用牌指定其以外的角色为目标时，你可令其摸一张牌并展示之并取消此牌一个目标，若两牌花色相同，其本回合下次使用此花色的牌选择目标后，其可以额外指定一个目标。",

  ["#wk_heg__jiexun-choose-add"] = "诫训：你可以为 %arg 额外指定一个合法目标",
  ["#wk_heg__jiexun-choose-cancel"] = "诫训：请为 %arg 取消一个目标",
  ["@wk_heg__jiexun-turn"] = "诫训",
  ["#wk_heg__jiexun_trigger"] = "诫训",

  ["$wk_heg__jiexun1"] = "帝王应以社稷为重，以大观为主。",
  ["$wk_heg__jiexun2"] = "吾冒昧进谏，只求陛下思虑。",
}

local H = require "packages/ol_hegemony/util"

jiexun:addEffect(fk.TargetSpecifying, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    if not (player:hasSkill(self) and H.compareKingdomWith(player, target) and data.firstTarget) then return false end
    if #data.use.tos == 1 and data.use.tos[1] == target then
      return false
    end
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local card = target:drawCards(1, jiexun.name)
    target:showCards(card)
    local targets = data.use.tos
    local tos = #targets == 1 and targets or room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = jiexun.name,
      prompt = "#wk_heg__jiexun-choose-cancel:::" .. data.card:toLogString(),
      cancelable = false,
    })
    data:cancelTarget(tos[1])
    if Fk:getCardById(card[1]).suit == data.card.suit then
      local mark = target:getTableMark("@wk_heg__jiexun-turn")
      if table.insertIfNeed(mark, Fk:getCardById(card[1]):getSuitString(true)) then
        room:setPlayerMark(target, "@wk_heg__jiexun-turn", mark)
        mark = target:getTableMark("_wk_heg__jiexun-turn")
        mark[Fk:getCardById(card[1]):getSuitString(true)] = player.id
        room:setPlayerMark(target, "_wk_heg__jiexun-turn", mark)
      end
    end
  end,
})


jiexun:addEffect(fk.AfterCardTargetDeclared, {
  is_delay_effect = true,
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    if target:getMark("@wk_heg__jiexun-turn") == 0 then return false end
    return target:getTableMark("_wk_heg__jiexun-turn")[data.card:getSuitString(true)] == player.id and data.card.trueName ~= "threaten_emperor"
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local targets = data:getExtraTargets()
    local to = room:askToChoosePlayers(target, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = jiexun.name,
      prompt = "#wk_heg__jiexun-choose-add:::" .. data.card:toLogString(),
      cancelable = true,
    })
    if #to > 0 then data:addTarget(to[1]) end
    local mark = target:getTableMark("@wk_heg__jiexun-turn")
    table.removeOne(mark, data.card:getSuitString(true))
    -- if #mark == 0 then mark = 0 end
    room:setPlayerMark(target, "@wk_heg__jiexun-turn", mark)
    mark = target:getTableMark("_wk_heg__jiexun-turn")
    mark[data.card:getSuitString(true)] = nil
    room:setPlayerMark(target, "_wk_heg__jiexun-turn", mark)
  end,
})

return jiexun